/*
 * Copyright 2025 The JA-SIG Collaborative. All rights reserved.
 * distributed with this file and available online at
 */
package com.lap.auth.adapter.driving.query.tenant.repository;

import com.lap.auth.adapter.driving.query.tenant.TenantQueryConverter;
import com.lap.auth.adapter.driving.query.tenant.mapper.TenantQueryMapper;
import com.lap.auth.adapter.driving.query.tenant.po.TenantQueryPo;
import com.lap.auth.application.tenant.dto.QueryTenantPageQuery;
import com.lap.auth.application.tenant.dto.TenantDto;
import com.lap.auth.application.tenant.repository.TenantQueryRepository;
import com.lap.framework.common.dto.page.PageResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@RequiredArgsConstructor
@Repository
public class TenantQueryRepositoryImpl implements TenantQueryRepository {

  private final TenantQueryConverter tenantQueryConverter;
  private final TenantQueryMapper tenantQueryMapper;

  @Override
  public PageResponse<TenantDto> findByConditionPaged(QueryTenantPageQuery query) {
    long count = tenantQueryMapper.countPageByCondition(query);
    if (count == 0L) {
      return PageResponse.ok(count);
    }
    List<TenantQueryPo> list = tenantQueryMapper.selectPageByCondition(query);
    return PageResponse.ok(tenantQueryConverter.toDtoList(list), count);
  }
}
